#coding=utf-8

import socket

def main():
    # 1. create socket
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # 2. bind a local information
    localaddr = ("", 17788)
    udp_socket.bind(localaddr)
    # 3. receive data
    while True:
        recv_data = udp_socket.recvfrom(1024)
        # recv_data storage is a tuples:(receive data, (send_ip, send_port))
        recv_msg = recv_data[0]
        send_addr = recv_data[1]
        # 4. print receive data
        # print(recv_data)
        print("%s:%s" % (str(send_addr), recv_msg.decode('gbk')))
        if recv_data == "exit":
            break
    # 5. close scoket
    udp_socket.close()


if __name__ == "__main__":
    main()